package com.atguigu.gmall.order.config;

import com.atguigu.gmall.rabbit.constant.RabbitConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DeadRabbitMQConfig {


    //创建ttl队列
    @Bean(RabbitConst.QUEUE_DEAD_1)
    public Queue createQueue(){
        return QueueBuilder.durable(RabbitConst.QUEUE_DEAD_1).
                deadLetterExchange(RabbitConst.EXCHANGE_DEAD).
                deadLetterRoutingKey(RabbitConst.ROUTING_DEAD_2).ttl(2*60*60*1000).build();
    }

    //创建交换机
    @Bean(RabbitConst.EXCHANGE_DEAD)
    public Exchange createExchange(){
        return ExchangeBuilder.directExchange(RabbitConst.EXCHANGE_DEAD).build();
    }

    //创建绑定
    @Bean
    public Binding createBinding(@Qualifier(RabbitConst.QUEUE_DEAD_1) Queue queue,
                                 @Qualifier(RabbitConst.EXCHANGE_DEAD) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RabbitConst.ROUTING_DEAD_1).noargs();
    }

    //创建死信队列
    @Bean(RabbitConst.QUEUE_DEAD_2)
    public Queue createDeadQueue(){
        return QueueBuilder.durable(RabbitConst.QUEUE_DEAD_2).build();
    }

    //死信队列绑定交换机
    @Bean
    public Binding createDeadBinding(@Qualifier(RabbitConst.QUEUE_DEAD_2) Queue queue,
                                     @Qualifier(RabbitConst.EXCHANGE_DEAD) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RabbitConst.ROUTING_DEAD_2).noargs();

    }


}
